<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8" />
		<title></title>
		<link type="text/css" rel="stylesheet" href="layui/css/layui.css" />
		<script type="text/javascript" src="layui/layui.js"></script>
	</head>
	<body>
		
		
		<div id="test1"></div>
		<div id="test2"></div>
		<script>
			layui.use(['transfer'],function(){
				var transfer = layui.transfer,$ = layui.$;
				
				//var userRole = new Array;
				
				// uid 获取添加用户 /登录 的时候
				var uid = "2b2b3f3a-61a1-4123-b6a6-9535ed1a4699";
				var token = "23d66d84-fae1-45d5-bd07-b694662fe579";
				// 1 做请求用户当前角色    /api/GetRolesByUserIdLayUI
				var userRole = new Array;
				
				
				$.post(
					'http://stuapi.ysdjypt.com/api/GetRolesByUserIdLayUI',
					{
						"uid":uid,
						"token":token
					},
					function(res){
						console.log(res.data);
						for (var i = 0; i < res.data.length; i++) {
							 userRole.push(res.data[i].Id);
						}
						
					},
					"json"
				);
				
				
				
				
				$.post(
					"http://stuapi.ysdjypt.com/api/GetRolesAll",
					{"token":token},
					function(res){
						console.log(res);
						transfer.render({
							elem:"#test1",
							data:res,
							value:userRole,
							title:["系统所有角色","当前用户的角色"],
							parseData:function(res){
								return {
									"value":res.Id,
									"title":res.Name
								}
							},
							id:"testTransfer",
							onchange:function(obj,index){
								// index 0  从左往右移动数据  给用户添加角色
								//       1 从右往左移动数据   删除角色
								console.log(obj);
								console.log(index);
								if(index == 0){
									//layer.alert("添加角色");
									// 请求添加角色地址  api/AddUserToRole
									for (var i = 0; i < obj.length; i++) {
										$.post(
											'http://stuapi.ysdjypt.com/api/AddUserToRole',
											{
												'uId':uid,
												"rId":obj[i].value,
												"token":token
											},
											function(res){
											},
											'json'
										)
									}
									
								}else{
									// 请求删除角色地址 /api/RemoveUserFromRole
										for (var i = 0; i < obj.length; i++) {
										$.post(
											'http://stuapi.ysdjypt.com/api/RemoveUserFromRole',
											{
												'uId':uid,
												"rId":obj[i].value,
												"token":token
											},
											function(res){
											},
											'json'
										)
									}
								}
								
							}
						})
					},
					"json"
				);
				
				
				
				
				
				
				
				
				
				
				
//				
//				transfer.render({
//					elem:"#test1",
//					title:["1901","1902"],
//					id:"testTransfer"
//				})
				
			})
		</script>
	</body>
</html>
